<!--
 * Copyright © 2022 XXXXXXXXX
 * @Author: zhangxiang
 * @Date: 2022-07-19 09:42:10
 * @LastEditors: zhangxiang
 * @LastEditTime: 2022-10-31 17:41:53
 * @Description:
 *
-->
<template>
  <div
    v-if="isExternal"
    :style="styleExternalIcon"
    class="svg-external-icon svg-icon"
    v-on="$listeners"
  ></div>
  <svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners">
    <use :xlink:href="iconName" />
  </svg>
</template>

<script>
// doc: https://panjiachen.github.io/vue-element-admin-site/feature/component/svg-icon.html#usage
import { isExternal } from '@/utils/validate'

export default {
  name: 'SvgIcon', // 组件名称
  props: {
    iconClass: {
      // icon的样式类
      type: String,
      required: true
    },
    className: {
      // 类名
      type: String,
      default: ''
    }
  },
  computed: {
    /**
     * @description: 是否外链
     * @return {*}
     */
    isExternal() {
      return isExternal(this.iconClass)
    },
    /**
     * @description: icon名称
     * @return {*}
     */
    iconName() {
      return `#icon-${this.iconClass}`
    },
    /**
     * @description: svg的类名
     * @return {*}
     */
    svgClass() {
      if (this.className) {
        return 'svg-icon ' + this.className
      } else {
        return 'svg-icon'
      }
    },
    /**
     * @description: 外链icon样式
     * @return {*}
     */
    styleExternalIcon() {
      return {
        mask: `url(${this.iconClass}) no-repeat 50% 50%`,
        '-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`
      }
    }
  }
}
</script>

<style scoped>
.svg-icon {
  width: 15px;
  height: 15px;
  /* width: 1em;
  height: 1em;
  vertical-align: -0.15em; */
  fill: currentColor;
  overflow: hidden;
}

.svg-external-icon {
  background-color: currentColor;
  mask-size: cover !important;
  display: inline-block;
}
</style>
